﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Truron.NResource.Interface;
using Truron.NResource.Api.Utility;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Truron.NResource.Model;
using Newtonsoft.Json;
using Truron.Utility;
using Newtonsoft.Json.Linq;
using Truron.SysCenter.Interface.Remote;

namespace Truron.NResource.Api.Controllers
{
     
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class NResource_ExpressAddressController : ControllerBase
    {
        private readonly INResource_ExpressAddressService _NResource_ExpressAddressService;
        private readonly ISysCenterRemoteService _SysCenterRemoteService;
        public NResource_ExpressAddressController(INResource_ExpressAddressService NResource_ExpressAddressService, ISysCenterRemoteService SysCenterRemoteService)
        {
            _NResource_ExpressAddressService = NResource_ExpressAddressService;
            _SysCenterRemoteService = SysCenterRemoteService;
        }
        [HttpPost]
        public IActionResult GetList(LayPageModel param)
        {
            var userToken = _SysCenterRemoteService.GetUserToken(HttpContext);
            int pageSize = Convert.ToInt32(param.limit), pageIndex = Convert.ToInt32(param.page);
            int pageNum = (pageIndex - 1) * pageSize;
             var List =  _NResource_ExpressAddressService.GetListAll();
              if (!string.IsNullOrEmpty(param.searchKey))
              {
              }
               var queryParam = param.searchJson.ToJObject();
                 if (!queryParam["beginTime"].IsEmpty())
            {
                List = List.FindAll(x => x.CreateTime >= Convert.ToDateTime(queryParam["beginTime"]));
            }
            if (!queryParam["endTime"].IsEmpty())
            {
                List = List.FindAll(x => x.CreateTime <= Convert.ToDateTime(queryParam["endTime"]));
            }
            var treeSearchJson = param.treeSearchJson;
             if (!treeSearchJson.IsEmpty())//BussinessType:194|CreateUser:27
            {

                string[] conditionarr = treeSearchJson.Split('|');
                for (int i = 0; i < conditionarr.Length; i++)
                {

                    if (conditionarr[i].IndexOf("SavePoint") >= 0)
                    {
                        //List = List.FindAll(x => x.SavePoint == Convert.ToInt32(conditionarr[i].Split(':')[1].ToString()));
                    }
                }
            }
              int Count = List.Count;
           // List = List.OrderByDescending(x => x.CreateTime).ToList();
            var ParamList = List.Skip(pageNum).Take(pageSize).ToList();
           return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = ParamList, count = Count }));
        }
        [HttpPost]
        public IActionResult Save(RecParam param)
        {
            NResource_ExpressAddress entity = JsonConvert.DeserializeObject<NResource_ExpressAddress>(param.strEntity);
            var userToken = _SysCenterRemoteService.GetUserToken(HttpContext);
            var resultEntity = _NResource_ExpressAddressService.Save(entity, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = resultEntity, code=0 }));
        }
        public IActionResult GetDetail(NResource_ExpressAddress param)
        {
            if (param.Id <= 0)
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            var ent = _NResource_ExpressAddressService.GetDetail(param.Id);
            if (ent == null)
                return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = null }));
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = ent,code=0 }));
        }
        public class RecParam
        {
            public string strEntity { get; set; }
        }
        [HttpPost]
        public IActionResult Del(RecParam param)
        {
            if (param.strEntity.Length == 0)
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            List<int> ids = JsonConvert.DeserializeObject<List<int>>(param.strEntity);
            var userToken = _SysCenterRemoteService.GetUserToken(HttpContext);
            var resultCode =  _NResource_ExpressAddressService.Del(ids, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = resultCode, code=0 }));
        }
        
    }
}